<?php
// +----------------------------------------------------------------------
// | OneKeyAdmin [ Believe that you can do better ]
// +----------------------------------------------------------------------
// | Copyright (c) 2020-2023 http://onekeyadmin.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: MUKE <513038996@qq.com>
// +----------------------------------------------------------------------
namespace plugins\form\admin\controller;

use think\facade\View;
use think\exception\ValidateException;
use app\admin\BaseController;
use app\admin\model\UserGroup;
use plugins\form\admin\model\Set as SetModel;
use plugins\form\admin\validate\Set as SetValidate;
/**
 * 表单设置
 */
class Set extends BaseController
{
    /**
     * 显示资源列表
     */
    public function index()
    {
    	if ($this->request->isPost()) {
            $input = input('post.');
            $search = ['keyword','status'];
            $count = SetModel::withSearch($search, $input)->count();
            $data  = SetModel::withSearch($search, $input)->order($input['prop'], $input['order'])->page($input['page'], $input['pageSize'])->select();
            return json(['status' => 'success', 'message' => '获取成功', 'data' => $data, 'count' => $count]);
    	} else {
            // 用户权限
            $group = UserGroup::where('status', 1)->order('integral', 'asc')->select();
            View::assign('group', $group);
    		return View::fetch();
    	}
    }

    /**
     * 保存新建的资源
     */
    public function save()
    {
        if ($this->request->isPost()) {
            try {
                $input = input('post.');
                validate(SetValidate::class)->scene('save')->check($input);
            } catch ( ValidateException $e ) {
                return json(['status' => 'error', 'message' => $e->getError()]);
            }
            SetModel::create($input);
            return json(['status' => 'success', 'message' => '新增成功']);       
        }
    }

    /**
     * 保存更新的资源
     */
    public function update()
    {
        if ($this->request->isPost()) {
            try {
                $input = input('post.');
                validate(SetValidate::class)->scene('update')->check($input);
            } catch ( ValidateException $e ) {
                return json(['status' => 'error', 'message' => $e->getError()]);
            }
            SetModel::update($input);
            return json(['status' => 'success', 'message' => '修改成功']);
        }
    }

    /**
     * 删除指定资源
     */
    public function delete()
    {
        if ($this->request->isPost()) {
            SetModel::destroy(input('post.ids'));
            return json(['status' => 'success', 'message' => '删除成功']);
        }
    }
}